package com.hebeiwangxiaozhang.pojo;

import com.hebeiwangxiaozhang.utils.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CommodityOnSaleBody {
    private List<String> columns;
    private List<Object> values;
    private List<CommodityAttributes> commonAttrs;

    /*
    [
                "TO DO",
                "E-SERIES 白色 39",
                "耐克",
                "运动鞋",
                "鞋子+鞋带+包装盒",
                "售后三包",
                494,
                1,
                16,
                103,
                9,
                43,
                [
                    {
                        "attrGroupId": 1,
                        "attrGroupName": "主体",
                        "attrId": 12,
                        "attrName": "颜色",
                        "attrValue": "白色"
                    },
                    {
                        "attrGroupId": 1,
                        "attrGroupName": "主体",
                        "attrId": 13,
                        "attrName": "尺码",
                        "attrValue": "39"
                    }
                ]
            ]
     */
    public List<CommodityEsBody> convertToEsBody() {
        List<CommodityEsBody> esBodies = new ArrayList<>();
        values.forEach(val -> {
            List<Object> valueList = JsonUtils.readValueFromObject(val, List.class);
            Map<String, Object> map = IntStream.range(0, columns.size()).boxed().collect(
                    Collectors.toMap(columns::get, valueList::get)
            );
            CommodityEsBody body = JsonUtils.readValueFromObject(map, CommodityEsBody.class);
            body.getAttrs().addAll(commonAttrs);
            esBodies.add(body);
        });
        return esBodies;
    }
}
